package com.gitee.shigen.lucene.service.impl;

import com.gitee.shigen.lucene.dao.ContentDao;
import com.gitee.shigen.lucene.service.LuceneService;
import com.gitee.shigen.lucene.vo.Content;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author shigenfu
 */
@Service
@Slf4j
public class LuceneServiceImpl implements LuceneService {

    @Resource
    private ContentDao contentDao;

    @Override
    public List<Content> getFakeData(Integer count) {
        count = 5;
        List<Content> dataList = new ArrayList<>();

        String[] titles = {"iPhone 13", "三星 Galaxy S21", "小米 Mi 11", "华为 P50", "一加 9 Pro"};
        String[] prices = {"￥999", "￥799", "￥899", "￥1099", "￥1199"};
        String[] images = {"https://example.com/image1.jpg", "https://example.com/image2.jpg", "https://example.com/image3.jpg", "https://example.com/image4.jpg", "https://example.com/image5.jpg"};
        String[] descriptions = {"最新的 iPhone，具有许多新功能", "一款面向游戏玩家的强大安卓手机", "一款拥有出色相机性能的旗舰手机", "华为的高端设备", "一款快速流畅的 OxygenOS 手机"};

        for (int i = 0; i < count; i++) {
            Content content = new Content();
            content.setId(i + 1);
            content.setTitle(titles[i]);
            content.setPrice(prices[i]);
            content.setImg(images[i]);
            content.setDesc(descriptions[i]);

            dataList.add(content);
        }
        return dataList;
    }

    @Override
    public Long save(List<Content> contentList) {
        long count = 0;
        try {
            count = contentDao.save(contentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }


    @Override
    public void deleteById(String id) {
        try {
            contentDao.deleteIndexById(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAll() {
        try {
            contentDao.deleteAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<Content> search(String[] fields, String keyword, int page, int pageSize) {
        return contentDao.search(fields, keyword, page, pageSize);
    }

    @Override
    public Content getDocumentById(String id) {
        try {
            return contentDao.getDocumentById(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
